package com.suptrack.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.suptrack.entity.Car;
import com.suptrack.entity.Position;
import com.suptrack.entity.Zone;
import com.suptrack.service.CarService;
import com.suptrack.service.PositionService;
import com.suptrack.service.ZoneService;

/**
 * Servlet implementation class removeCarServlet
 */
@WebServlet("/RegisteredContent/removeCar")
public class removeCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	CarService carservice;
	@EJB
	ZoneService zoneservice;
	@EJB
	PositionService positionservice;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		long id = Long.parseLong(request.getParameter("id2"));
		Car _car = carservice.getCarInfoService(id);
		List<Zone> listzone = _car.getZone();
		for(Zone zone : listzone)
		{
			zoneservice.deleteZoneService(zone);
		}
		List<Position> listposition = _car.getPosition();
		for(Position position : listposition)
		{
			positionservice.deletePositionService(position);
		}
		carservice.deleteCarService(_car);
		
		response.sendRedirect(request.getServletContext().getContextPath()+"/RegisteredContent/ManageCar");
				}

}
